package com.voxel.sense.infomsg.net.helper;

import android.support.annotation.NonNull;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.FileUtils;
import com.voxel.sense.infomsg.net.FileOperatorRemoteService;
import com.voxel.sense.infomsg.net.UpDownNetwork;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.constants.CommonConstants;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.net.RequestIdFactory;
import cyy.example.com.facotry.net.helper.RspCallback;
import cyy.example.com.facotry.share.SPEngine;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by ChenYiYao on 2018/1/22.
 */

public class FileOperatorNetHelper {
    public static final String MULTIPART_FORM_DATA = "multipart/form-data";

    //上传个人头像
    public static Call upPortrait(File file, DataSource.Callback callback) {
        FileOperatorRemoteService remote = UpDownNetwork.remote();
        MultipartBody.Part file1 = prepareFilePart("photo", file);
        String user_id = SPEngine.getSPEngine().getUserInfo().getUser_id();
        RequestBody body = toStringRequestBody(user_id);
        Call rspModelCall = remote.upLoadHeadFile(CommonConstants.API_URL_PREFIX+"accounts/user/uploadPhoto",body, file1);
        rspModelCall.enqueue(new RspCallback(callback, RequestIdFactory.QUERY_FIRM_LIST_INFO));
        return rspModelCall;
    }

    /**
     * 上传任务文件
     *
     * @param fileType   文件类型
     * @param taskNumber 任务编号
     * @param filePath   文件
     */
    public static Call upTaskFile(String fileType, String taskNumber,
                                  List<String> filePath, DataSource.Callback<BaseEntity> callback) {
        if (filePath == null || filePath.size() < 1) {
            return null;
        }
        FileOperatorRemoteService remote = UpDownNetwork.remote();
        Map<String, MultipartBody.Part> partMap = getprepareFilePartMap("files", filePath);
        RequestBody fileTypeBody = toStringRequestBody(fileType);
        RequestBody taskNumberBody = toStringRequestBody(taskNumber);
        Call upHeadEntityCall = remote.upLoadTaskFile(fileTypeBody, taskNumberBody, partMap);
        upHeadEntityCall.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.UP_TASK_FILE));
        return upHeadEntityCall;

    }


    private static RequestBody toStringRequestBody(String descriptionString) {
        //注意这里若是添加type，服务器不会接受这个参数
        return RequestBody.create(
                MediaType.parse(""), descriptionString);
    }


    private static Map<String, MultipartBody.Part> getprepareFilePartMap(String partName, List<String> list) {
        Map<String, MultipartBody.Part> partMap = new HashMap<>();
        if (list != null && list.size() > 0) {
            for (String path : list) {
                if (FileUtils.isFileExists(path)) {
                    MultipartBody.Part part = prepareFilePart(partName, new File(path));
                    partMap.put(path, part);
                }
            }
        }
        return partMap;
    }

    @NonNull
    private static MultipartBody.Part prepareFilePart(String partName, File file) {
        // 为file建立RequestBody实例
        RequestBody requestFile =
                RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
        // MultipartBody.Part借助文件名完成最终的上传
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }

}
